package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class JDBC_con {
	private Connection getConn() {
		Connection con = null;
		try {
			//1加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2建立连接
			con = DriverManager
					.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	public List<ResultSet> getHeros(String sql) {
		Connection conn = this.getConn();
		//3执行语句
		Statement st;
		List<ResultSet> list = null;
		int i;
		try {
			st = conn.createStatement();
			//4返回查询结果
			ResultSet sl=st.executeQuery(sql);
			while (sl.next()) {
				
				list.add(sl);
			}
			
//			i=st.executeUpdate(sql);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			try {
				//5关闭连接
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return list;
	}
	public static void main(String[] args) {
		JDBC_con a = new JDBC_con();
		List list = a.getHeros("select * from hero");
		System.out.println(list);
	}
}
